<template>
  <div class="result-container">
    <div class="title-wrap">
      <h2 class="title">{{$route.query.query}}</h2>
      <span class="sub-title">找到{{count}}个结果</span>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲" name="songs">
        <table class="el-table">
          <thead>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr
              class="el-table__row"
              @dblclick="play(item.id)"
              v-for="(item,key) in list"
              :key="item.key"
            >
              <td>{{key+1}}</td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span>{{item.name}}</span>
                    <span v-if="item.mvid!=0" class="iconfont icon-mv"></span>
                  </div>
                  <span v-if="item.alias?true:false">{{item.alias[0]}}</span>
                </div>
              </td>
              <td>{{item.artists[0].name}}</td>
              <td>{{item.album.name}}</td>
              <td>{{item.duration}}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane label="歌单" name="lists">
        <div class="items">
          <div class="item" v-for="item in playlists" :key="item.key" @click="toplaylist(item.id)">
            <div class="img-wrap">
              <div class="num-wrap">
                播放量:
                <span class="num">{{item.playCount}}</span>
              </div>
              <img :src="item.coverImgUrl" alt />
              <span class="iconfont icon-play"></span>
            </div>
            <p class="name">{{item.name}}</p>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="MV" name="mv">
        <div class="items mv">
          <div class="item" v-for="item in mvs" :key="item.key" @click="mvplay(item.id)">
            <div class="img-wrap">
              <img :src="item.cover" alt />
              <span class="iconfont icon-play"></span>
              <div class="num-wrap">
                <div class="iconfont icon-play" ></div>
                <div class="num">{{item.playCount}}</div>
              </div>
              <span class="time">{{item.time}}</span>
            </div>
            <div class="info-wrap">
              <div class="name">{{item.name}}</div>
              <div class="singer">{{item.artistName}}</div>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "result",
  data() {
    return {
      mvs: [],
      playlists: [],
      count: 0,
      activeIndex: "songs",
      list: "songs"
    };
  },
  methods: {
    play(id) {
      console.log(id);

      axios({
        url: "https://autumnfish.cn/song/url",
        params: {
          id
        }
      }).then(res => {
        let url = res.data.data[0].url;
        this.$parent.songurl = url;
      });
    },
    toplaylist(id) {
      console.log(id);
      this.$router.push("/playlist?q=" + id);
    },
    mvplay(id) {
      console.log(id);
      this.$router.push("/mv?q=" + id);
    }
  },
  watch: {
    activeIndex() {
      let type = 1;
      switch (this.activeIndex) {
        case "songs":
          type = 1;
          break;
        case "lists":
          type = 1000;
          break;
        case "mv":
          type = 1004;
          break;
        default:
          break;
      }
      if (type == 1) {
        axios({
          url: "https://autumnfish.cn/search",
          params: {
            limit: 5,
            keywords: this.$route.query.query
          }
        }).then(res => {
          console.log(res);
          this.list = res.data.result.songs;
          this.count = res.data.result.songCount;
          for (let i = 0; i < this.list.length; i++) {
            let time = parseInt(this.list[i].duration / 1000);
            let sec = parseInt((this.list[i].duration / 1000) % 60);
            let min = parseInt(time / 60);
            this.list[i].duration = min + ":" + sec;
            console.log(this.list[i].duration);
          }
        });
      } else if (type == 1000) {
        axios({
          url: "https://autumnfish.cn/search",
          params: {
            limit: 5,
            keywords: this.$route.query.query,
            type
          }
        }).then(res => {
          console.log(res);
          this.playlists = res.data.result.playlists;
          this.count = res.data.result.playlistCount;
        });
      } else {
        console.log("mv");
        axios({
          url: "https://autumnfish.cn/search",
          params: {
            limit: 5,
            keywords: this.$route.query.query,
            type
          }
        }).then(res => {
          console.log(res.data.result.mvs[0].id);
          this.mvs = res.data.result.mvs;
          this.count = res.data.result.mvCount;
          for (let i = 0; i < this.mvs.length; i++) {
            let time = parseInt(this.mvs[i].duration / 1000);
            let sec = parseInt((this.mvs[i].duration / 1000) % 60);
            let min = parseInt(time / 60);
            this.mvs[i].time = min + ":" + sec;
          }
        });
      }
    }
  },
  created() {
    axios({
      url: "https://autumnfish.cn/search",
      params: {
        limit: 5,
        keywords: this.$route.query.query
      }
    }).then(res => {
      console.log(res);
      this.list = res.data.result.songs;
      this.count = res.data.result.songCount;
      for (let i = 0; i < this.list.length; i++) {
        let time = parseInt(this.list[i].duration / 1000);
        let sec = parseInt((this.list[i].duration / 1000) % 60);
        let min = parseInt(time / 60);
        this.list[i].duration = min + ":" + sec;
        console.log(this.list[i].duration);
      }
    });
  }
};
</script>

<style >
</style>
